// packageA/pages/equipmentDetail/equipmentDetail.js
const app = getApp();
import CommonService from '../../../utils/service/commonService.js';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
const util = require('../../../utils/util');
import Base64 from '../../../utils/Base64.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //传参
    paramObj:null,
    //传参来的id
    id:null,
    //设备信息
    info:{},
    //价格
    priceList:[],
    //地面字典
    floorMap:null,
    floorUrlMap:null,
    //配置选择框
    showSku:false,
    //配置选择数据
    skuList:[],
    //选中的配置
    skuCurrent:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.paramObj){
      let paramObj=JSON.parse(options.paramObj);
      console.log('设备详情传参');
      console.log(paramObj);

      this.setData({
        paramObj: paramObj
      })

      if(paramObj.id){
        this.setData({
          id: paramObj.id
        })

        //查询字典-地面
        this.getFloors();
      }
    }
  },

  //获取设备信息
  getInfo(){
    let param = {
      isAuth: false,
      className: 'spu',
      data: {
        id: this.data.id,
        selectFields: ['keyAttr', 'unit', 'sellingPoint', 'specificationsArr', 'supplierArr', 'name','sku', 'grounds', 'imgUrl','workHours','efficiency','minEff','eqLong','eqWidth','eqHeight','clWidth'],
        nbl:Base64.encode(util.timeFormatDay(new Date()))
      }
    }

    CommonService.goFindList(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result[0];
        console.log('设备信息');
        console.log(result);  

        //清洁面积
        if(result.clWidth && result.efficiency && result.workHours){
          result.bigClearArea = (result.clWidth.value * result.efficiency.value * result.workHours.value)/1000;
          result.minClearArea = (result.clWidth.value * result.minEff.value * result.workHours.value)/1000;
        }

        this.setData({
          info: result
        })

        //配置信息
        if(result.sku && result.sku.length){
          let idx=0;
          result.sku.forEach((item,index)=> {
            if(item.defChecked){
              idx=index;
            }
          });

          console.log('当前配置');
          console.log(result.sku[idx]);
          
          this.setData({
            skuCurrent: result.sku[idx]
          })  

          //查询价格
          this.getPrice(result.sku[idx].id);
        }

        //地面信息
        if(result.grounds){
          this.getFloorWord(result.grounds);
        }

      }
    }).catch((error) => {
    })
  },

  //查询价格
  getPrice(id){
    let param = {
      isAuth: false,
      className: 'pricingStrategy',
      data: {
        skuId: id,
        type: 3,
        searchAll: true,
        selectFields: ['id', 'deposit', 'leaseTerm', 'rangeUnit','leasePrice','type'],
        nbl:Base64.encode(util.timeFormatDay(new Date()))
      }
    }

    CommonService.goFindList(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result;
        console.log('价格信息');
        console.log(result);

        this.setData({
          priceList: result
        })
      }
    }).catch((error) => {
    })
  },

  //查询字典-地面
  getFloors(){
    let param1 = {
      isAuth: false,
      className: 'dictionary',
      data: {
        searchAll: true,
        key: 'floors',
        selectFields: ['id','name','url'],
        nbl:Base64.encode(util.timeFormatDay(new Date()))
      }
    }

    CommonService.goFindList(param1).then(res=>{
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result;
        let map = new Map(),map2 = new Map();
        result.forEach(item => {
          map.set(item.id,item.name);
          map2.set(item.id,item.url);
        });
        this.setData({
          floorMap: map,
          floorUrlMap: map2
        })
      }

      //获取设备信息
      this.getInfo();

    }).catch((error) => {
    })
  },

  //处理地面
  getFloorWord(grounds){
    let names=this.data.floorMap;
    let urls=this.data.floorUrlMap;
   
    let arr=[];
    grounds.forEach(item => {
		  arr.push({
        id: item,
        name: names.get(item),
        url: urls.get(item)
      })
    });

    let groundInfo='info.groundInfo';
    this.setData({
      [groundInfo]:arr
    })
  },

  //显示配置选择框
  onShowSku(){
    let list=this.data.info.sku;
    if(list.length>1){
      let arr=[];
      list.forEach(item => {
        arr.push({
          text:item.name,
          id:item.id,
          obj:item
        });
      });
      this.setData({
        showSku: true,
        skuList:arr
      })
    }
  },

  //隐藏配置选择框
  onCloseSku(){
    this.setData({
      showSku: false
    })
  },

  //确认配置
  onConfirmSku(e){
    let obj=e.detail.value.obj;
    this.setData({
      skuCurrent:obj
    })

    //查询价格
    this.getPrice(obj.id);

    this.onCloseSku();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
